package com.boful.test;

import java.io.File;
import java.util.List;

import com.boful.convert.core.ConvertProviderConfig;
import com.boful.convert.core.impl.BofulConvertProvider;
import com.boful.convert.model.DiskFile;
import com.boful.convert.utils.MediaInfo;
import com.boful.convert.utils.TimeLengthUtils;

public class SplitConvert {
	public static void main(String[] args) throws Exception {
		
		final long startTime = System.currentTimeMillis();
		
		File file = new File("/home/ligson/.boful/convertServer/convert.xml");
		
		DiskFile src = new DiskFile("/home/ligson/1/ligson.avi");
		
		ConvertProviderConfig config = new ConvertProviderConfig();
		config.init(file);
		final BofulConvertProvider provider = new BofulConvertProvider(config);
		final MediaInfo mediaInfo = new MediaInfo(new File(provider.getMediaInfoPath()), src);
		System.out.println(mediaInfo);
		
		
		final int len = 10;
		List<List<Long>>  times = TimeLengthUtils.splitTime(mediaInfo.getVideoInfo().getDuration(), len);
		System.out.println(times);
		//provider.splitVideo(src, dest, startTime, endTime);
		final DiskFile[] files = new DiskFile[len];
		for(int i = 0;i<len;i++){
			DiskFile diskFile = new DiskFile(new File("/home/ligson/1",i+"."+src.getFileType()));
			files[i] = diskFile;
			List<Long>  timeList = times.get(i);
			//System.out.println(timeList.get(i)+"----"+timeList.get(1));
			provider.splitVideo(src, diskFile,timeList.get(0), timeList.get(1));
		}
		
		/*final DiskFile[] files2 = new DiskFile[len];
		final Thread[] threadArray = new Thread[len];
		for(int i = 0;i<len;i++){
			
			final int index = i;
			
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					DiskFile srcDiskFile = files[index];
					DiskFile tmp = new DiskFile(new File(srcDiskFile.getParent(),srcDiskFile.getFileName()+".mp4"));
					provider.transcodeVideo(srcDiskFile, tmp, new MyTranscodeEvent());
					files2[index] = tmp;
				}
			});
			thread.start();
			threadArray[i] = thread;
			
		}
		
		Thread deam = new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true){
					int count = 0;
					for(Thread thread :threadArray){
						if(!thread.isAlive()){
							count++;
						}
					}
					if(count==threadArray.length){
						break;						
					}
				}
				try {
					for(int i = 0;i<len;i++){
						System.out.println(i+"------------"+files2[i]);
					}
					provider.connectVideo(files2, new DiskFile("/home/ligson/1/final.mp4"));
					
					long endTime = System.currentTimeMillis();
					
					long useTime = (endTime-startTime)/1000;
					long duration = mediaInfo.getVideoInfo().getDuration();
					System.out.println("use:"+useTime);
					System.out.println("duration:"+duration);
					System.out.println("倍速："+duration/useTime);
					
					
					for(DiskFile tmp:files){
						tmp.deleteOnExit();
					}
					for(DiskFile tmp:files2){
						tmp.deleteOnExit();
					}
					
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		
		deam.start();
		*/
		
		
	}
}
